#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/10/9 0:49
# @Author : cjw
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

from testcases.login_test import TestLogin as Login


class TestCreateFolder(HttpRunner):

    config = Config("创建文件夹").verify(False).variables(**{
        "data_unique_id": "${get_unique_id()}",
    })

    teststeps = [
        Step(
            RunTestCase('login mubu')
            .with_variables(**{
                "phone": "18367157420",
                "password": "qwer128201209q@",
            })
            .call(Login)
            .export("token")
        ),
        Step(
            RunRequest("/v3/api/list/create_folder")
            .options("https://api2.mubu.com/v3/api/list/create_folder")
            .with_headers(
                **{
                    "accept": "*/*",
                    "access-control-request-method": "POST",
                    "access-control-request-headers": "content-type,data-unique-id,jwt-token,version,x-request-id",
                    "origin": "https://mubu.com",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-dest": "empty",
                    "referer": "https://mubu.com/",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
            .validate()
            .assert_equal("status_code", 200)
        ),
        Step(
            RunRequest("/v3/api/list/create_folder")
            .with_variables(**{
                "prefix": "cjw"
            })
            .post("https://api2.mubu.com/v3/api/list/create_folder")
            .with_headers(
                **{
                    "content-length": "34",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${get_unique_request_id()}",
                    "version": "3.0.0-2.0.0.1824",
                    "origin": "https://mubu.com",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "https://mubu.com/",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
            .with_json({"name": "${get_random_folder_name($prefix)}", "folderId": "0"})
            .teardown_hook("${sleep_random_seconds()}", "sleep_secs")
            .extract()
            .with_jmespath('body.data.folder.id', 'folderId')
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/notify/new_tip/get")
            .post("https://api2.mubu.com/v3/api/notify/new_tip/get")
            .with_headers(
                **{
                    "content-length": "10",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${get_unique_request_id()}",
                    "version": "3.0.0-2.0.0.1824",
                    "origin": "https://mubu.com",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "https://mubu.com/",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
            .with_json({"type": 1})
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/list/item_count")
            .post("https://api2.mubu.com/v3/api/list/item_count")
            .with_headers(
                **{
                    "content-length": "42",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${get_unique_request_id()}",
                    "version": "3.0.0-2.0.0.1824",
                    "origin": "https://mubu.com",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "https://mubu.com/",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
            .with_json({"folderId": "$folderId", "source": "home"})
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.code", 0)
        ),
    ]


if __name__ == "__main__":
    TestCreateFolder().test_start()